package com.fernfx.xingtan.common;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.ConsumerApplication;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.entity.robredpacket.RobredPacketInfoEntity;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.SJJNetworkProxy;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.main.ui.ChatFragment;
import com.fernfx.xingtan.main.ui.ContactsInfoFragment;
import com.fernfx.xingtan.robredpacket.ui.RobredPacketInfoActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.NetworkUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.utils.UIUtil;
import com.fernfx.xingtan.view.dialog.CustomDialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.msebera.android.httpclient.util.NetUtils;

import static com.baidu.location.d.j.P;

/**
 * Created by admin on 2018/8/10.
 */
public final class BaiduView implements BaiduMap.OnMarkerClickListener {

    private ChatFragment chatFragment;
    private BaiduMap mBaiduMap;
    LocationClient mLocationClient;
    // 当前经纬度
    double mLantitude;
    double mLongtitude;
    LatLng mLoactionLatLng;
    private boolean isFirstLoc = true; // 是否首次定位
    //城市
    private String city;

    public void initMap(final ChatFragment chatFragment,TextureMapView tMapView) {
        this.chatFragment = chatFragment;
        mBaiduMap = tMapView.getMap();
        //设置没有缩小放大按钮
        tMapView.showZoomControls(true);
        mBaiduMap.setOnMarkerClickListener(this);

        //默认显示普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //开启交通图
        //mBaiduMap.setTrafficEnabled(true);
        //开启热力图
        //mBaiduMap.setBaiduHeatMapEnabled(true);
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        //声明LocationClient类
        mLocationClient = new LocationClient(ConsumerApplication.getInstance());

        //配置定位SDK参数
        initLocation();
        //注册监听函数
        mLocationClient.registerLocationListener(new BDLocationListener() {

            @Override
            public void onReceiveLocation(BDLocation location) {
                // map view 销毁后不在处理新接收的位置
                if (location == null || mBaiduMap == null)
                    return;
                MyLocationData data = new MyLocationData.Builder()//
                        // .direction(mCurrentX)//
                        .accuracy(50)//
                        .latitude(location.getLatitude())//
                        .longitude(location.getLongitude())//
                        .build();
                mBaiduMap.setMyLocationData(data);
                // 设置自己定义图标
                MyLocationConfiguration config = new MyLocationConfiguration(
                        MyLocationConfiguration.LocationMode.NORMAL, true, null);
                mBaiduMap.setMyLocationConfigeration(config);

                mLantitude = location.getLatitude();
                mLongtitude = location.getLongitude();
                //城市
                if (TextUtils.isEmpty(city)) {
                    city = location.getCity();
                }

                mLoactionLatLng = new LatLng(mLantitude, mLongtitude);

                //请求红包接口
                chatFragment.showRobRedPacket(location);
                // 是否第一次定位
                if (isFirstLoc) {
                    isFirstLoc = false;
                    // 实现动画跳转
                    MapStatusUpdate u = MapStatusUpdateFactory
                            .newLatLng(mLoactionLatLng);
                    mBaiduMap.animateMapStatus(u);
                    LatLng ll = new LatLng(mLantitude, mLongtitude);
                    MapStatus.Builder builder = new MapStatus.Builder();
                    builder.target(ll).zoom(19.6f);
                    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
                    return;
                }
            }
        });
        //开启定位
        mLocationClient.start();
        //图片点击事件，回到定位点
        mLocationClient.requestLocation();
    }

    public BaiduMap getBaiduMap() {
        return mBaiduMap;
    }

    public LocationClient getLocationClient() {
        return mLocationClient;
    }

    public String getCity(){
        return city;
    }

    public LatLng getLoactionLatLng(){
        return mLoactionLatLng;
    }

    //配置定位SDK参数
    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = Constant.ApplicationVariable.SPAN;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation
        // .getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);
        option.setOpenGps(true); // 打开gps

        //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要
        mLocationClient.setLocOption(option);
    }

    //点击红包
    @Override
    public boolean onMarkerClick(Marker marker) {
        Bundle bundle = marker.getExtraInfo();
        if (bundle.containsKey("id")) {
            CustomDialog customDialog = new CustomDialog(chatFragment.getActivity());
            customDialog.setCanceledOnTouchOutside(true);
            customDialog.setMarker(marker);
            customDialog.show();
         /*   int id = bundle.getInt("id");
            marker.remove();*/
        }
        return false;
    }
}
